package com.example.ljw.frescowallpaper.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.base.BaseActivity;
import com.example.ljw.frescowallpaper.bean.UserBean;
import com.example.ljw.frescowallpaper.config.Constants;
import com.example.ljw.frescowallpaper.utils.PreUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends BaseActivity {

    private EditText userId_register,userName_register,userpsw1_register,userpsw2_register;
    private Button registerBtn_register;
    private UserBean user;
    private String userName,userPsw1,userPsw2,userId;
    private String objectId;
    private List<String> favoriteList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        zhuCe();
    }

    private void zhuCe() {
        registerBtn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                user = new UserBean();
                userId = userId_register.getEditableText().toString();
                userName = userName_register.getEditableText().toString();
                userPsw1 = userpsw1_register.getEditableText().toString();
                userPsw2 = userpsw2_register.getEditableText().toString();
                favoriteList = new ArrayList<String>();
                favoriteList.add("");
                boolean matches = userId.matches("^[a-zA-z][a-zA-Z0-9_]{2,9}$");
                if(userId.equals("")||userPsw1.equals("")||userPsw2.equals("")||userName.equals(""))
                {
                    Toast.makeText(RegisterActivity.this, "内容不能为空", Toast.LENGTH_LONG).show();
                    return;
                }else if (matches&&userPsw1!=userPsw2){
                    user.setUserID(userId);
                    user.setUserPsw(userPsw1);
                    user.setUserName(userName);
                    user.setFavorite(favoriteList);

                    user.setIconUrl("http://p1.bpimg.com/567571/ec46f53f67af6efc.png");
                    user.save(new SaveListener<String>() {
                        @Override
                        public void done(String ObjectId, BmobException e) {
                            if(e==null){
                                objectId = ObjectId;
//                                PreUtils.writeString(RegisterActivity.this, Constants.KEY_OF_OBJECTID,objectId);
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }else{
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                }else {
                    Toast.makeText(RegisterActivity.this, "账号不符合规范", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        userId_register = (EditText) findViewById(R.id.userid_register);
        userName_register = (EditText) findViewById(R.id.username_register);
        userpsw1_register = (EditText) findViewById(R.id.psw1_register);
        userpsw2_register = (EditText) findViewById(R.id.psw2_register);
        registerBtn_register = (Button) findViewById(R.id.register_btn);

    }
}
